package com.mall.mymall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.mymall.common.Result;
import com.mall.mymall.dao.dto.CartsDto;
import com.mall.mymall.dao.entity.Carts;
import com.mall.mymall.dao.entity.Users;
import com.mall.mymall.service.CartsService;
import com.mall.mymall.service.UsersService;
import com.mall.mymall.utils.JwtTokenProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.net.http.HttpRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 十里坡
 * @since 2025-03-06
 */
@Controller
@RequestMapping("/carts")
public class CartsController {

    @Autowired
    UsersService usersService;

    @Autowired
    CartsService cartsService;

    @Autowired
    JwtTokenProvider jwtTokenProvider;

    @RequestMapping("/getAll")
    @ResponseBody
    public Result<List<CartsDto>> getCarts(HttpServletRequest request) {
        String username = jwtTokenProvider.getUsernameFromToken(jwtTokenProvider.getJwtFromRequest(request));
        if (username == null) {
            return Result.fail("请先登录");
        }
        Users userByUsername = usersService.getUserByUsername(username);
        Integer userId = userByUsername.getUserId();
        List<CartsDto> carts = cartsService.getCarts(userId);
        return Result.success(carts);
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result<Carts> addCart(HttpServletRequest request, Integer productId, Integer quantity) {
        String username = jwtTokenProvider.getUsernameFromToken(jwtTokenProvider.getJwtFromRequest(request));
        if (username == null) {
            return Result.fail("请先登录");
        }
        Users userByUsername = usersService.getUserByUsername(username);
        Integer userId = userByUsername.getUserId();
        Carts carts = new Carts(null, userId, productId, quantity);
        Carts one = cartsService.getOne(new QueryWrapper<Carts>().eq("user_id", userId).eq("product_id", productId));
        System.out.println(one);
        if (one != null) {
            one.setQuantity(one.getQuantity() + quantity);
            cartsService.update(one, new QueryWrapper<Carts>().eq("cart_id", one.getCartId()));
            return Result.success(one);
        } else {
            cartsService.save(carts);
            return Result.success(carts);
        }
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result<Carts> updateCart(HttpServletRequest request, @RequestParam Integer cartId, @RequestParam Integer quantity) {
        String username = jwtTokenProvider.getUsernameFromToken(jwtTokenProvider.getJwtFromRequest(request));
        if (username == null) {
            return Result.fail("请先登录");
        }
        Users userByUsername = usersService.getUserByUsername(username);
        Integer userId = userByUsername.getUserId();
        Carts carts = new Carts(cartId, userId, null, quantity);
        QueryWrapper<Carts> cartsQueryWrapper = new QueryWrapper<>();
        cartsQueryWrapper.eq("cart_id", cartId);
        cartsService.update(carts, cartsQueryWrapper);
        return Result.success(carts);
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Result<String> deleteCart(HttpServletRequest request, @RequestParam Integer cartId) {
        QueryWrapper<Carts> cartsQueryWrapper = new QueryWrapper<>();
        cartsQueryWrapper.eq("cart_id", cartId);
        cartsService.remove(cartsQueryWrapper);
        return Result.success("删除成功");
    }



    @RequestMapping("/deleteAll")
    @ResponseBody
    public Result<String> deleteAllCart(HttpServletRequest request) {
        String username = jwtTokenProvider.getUsernameFromToken(jwtTokenProvider.getJwtFromRequest(request));
        if (username == null) {
            return Result.fail("请先登录");
        }
        Users userByUsername = usersService.getUserByUsername(username);
        Integer userId = userByUsername.getUserId();
        QueryWrapper<Carts> cartsQueryWrapper = new QueryWrapper<>();
        cartsQueryWrapper.eq("user_id", userId);
        cartsService.remove(cartsQueryWrapper);
        return Result.success("删除成功");
    }
}

